package com.jivam.lottery.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.jivam.lottery.data.DataCalculator;
import com.jivam.lottery.data.DataUtil;
import com.jivam.lottery.model.KeyFormula;
import com.jivam.lottery.model.PeriodNum;
import com.jivam.lottery.panel.FormulaPanel;
import com.jivam.lottery.panel.NumPanel;
import com.jivam.lottery.panel.PeriodPanel;
import com.jivam.lottery.util.Constants;

/**
 * 彩票统计工具主面板类
 * @author Jivam
 */
public class Analyse extends JFrame implements ActionListener{

	private static final long serialVersionUID = -5629290145965358626L;
	
	private JTextField periodtext;
	private JTextField idtext;
	private JButton button;
	private NumPanel numPanel;
	
	private static Analyse analyse;
	public static Analyse getInstanse(){
		if(analyse == null){
			analyse = new Analyse();
		}
		return analyse;
	}
	
	private Analyse() {
		super(Constants.TOOL_ANALYSE+Constants.VERSION+Constants.AUTHOR_NAME);
		init();//初始化图形
		addEventHandle();//初始化时间关联
	}
	
	private void init() {
		JTabbedPane periodPane = new JTabbedPane();
		JTabbedPane analysisPane = new JTabbedPane();
		
		PeriodPanel periodPanel = new PeriodPanel();
		periodPane.addTab("历史期数", periodPanel);
		periodPane.setSelectedIndex(0);
		
		numPanel = new NumPanel();
		analysisPane.addTab("期数统计", numPanel);
		analysisPane.setSelectedIndex(0);

		FormulaPanel fp = null;
		for(KeyFormula kf :DataCalculator.getFdata()){
			fp = new FormulaPanel(kf);
			analysisPane.addTab(kf.getKey(), fp);
		}
		
		JPanel south = new JPanel();
		south.setLayout(new FlowLayout());
		periodtext = new JTextField(10);
		idtext = new JTextField(DataCalculator.getNextPid()+"", 5);
		button = new JButton("提交");
		south.add(idtext);
		south.add(new JLabel("期七位号码："));
		south.add(periodtext);
		south.add(button);
		
		BorderLayout mainLayout = new BorderLayout();
		mainLayout.setHgap(5);
		this.setLayout(mainLayout);
		this.add(periodPane ,BorderLayout.WEST);
		this.add(analysisPane ,BorderLayout.CENTER);
		this.add(south,BorderLayout.SOUTH);
		this.setSize(DataUtil.getData(Constants.KEY_ANALYSE_WIDTH,820),DataUtil.getData(Constants.KEY_ANALYSE_HEIGHT,515));
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//居中
		this.setLocation((screenSize.width-getWidth())/2,(screenSize.height-getHeight())/2);
//		this.setAlwaysOnTop(true);
//		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//关闭按钮
	}
	
	private void addEventHandle() {//添加事件监听
		button.addActionListener(this);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				numPanel.saveFile();
				Lottery.analyseFlag = false;
				Lottery.close();
			}
		});
		this.addComponentListener(new ComponentAdapter(){
			@Override
			public void componentResized(ComponentEvent e) {
				DataUtil.addData(Constants.KEY_ANALYSE_WIDTH, Analyse.getInstanse().getWidth());
				DataUtil.addData(Constants.KEY_ANALYSE_HEIGHT, Analyse.getInstanse().getHeight());
				DataUtil.saveData();
			}
		}); 
	}


	public void actionPerformed(ActionEvent ae) {
		String numText = null;
		int num = -1;
		int id = -1;
		try {
			numText = periodtext.getText().trim();
			numText = numText.substring(0, 7);
			num = Integer.parseInt(numText);
			id = Integer.parseInt(idtext.getText());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "您输入的期数 或 号码不符合要求！");
		}
		//是0 否1
		if(num > -1 && id > -1){
			if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, "您输入的 "+id+" 期的号码为 "+numText, "输入确认",JOptionPane.YES_NO_OPTION )){
				PeriodNum period = new PeriodNum(num,id);
				DataCalculator.addPeriod(period);
				reloadPanel();
				Calculate.getInstanse().flash();
			}
		}
	}
	
	public void reloadPanel(){
		periodtext.setText("");
		idtext.setText(DataCalculator.getNextPid()+"");
	}
	
	

}
